/******************
指标拟定
* */
<template>
  <basic-container>
    <el-row>
      <targetExaminHead :sign="{ a: false, b: true, c: false,d: false,e: false}" />
    </el-row>
    <el-row v-if="!isPlanSummary">
      <el-row style="margin:25px 0 0 0 ;">
        <div :height="searchheight">
          <span
            style="float:right;margin-top: -30px;cursor: pointer;"
            @click="searchchangeadvanced"
          >
            {{ searchadvanced ? "查询条件收起" : "查询条件展开" }}
            <i
              :class="searchadvanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
            />
          </span>
          <div v-show="searchadvanced">
            <el-form
              :inline="true"
              :model="searchForm"
              @keyup.enter.native="getDataList()"
              class="form-inline"
            >
              <el-form-item label="考核对象">
                <el-input v-model="searchForm.assessRealName" placeholder="考核对象" :clearable="true"></el-input>
              </el-form-item>
              <el-form-item label="考核名称">
                <el-input v-model="searchForm.name" placeholder="考核名称" :clearable="true"></el-input>
              </el-form-item>
              <el-form-item label="考核周期">
                <selectSearch @getDataByPerid="getDataByPerid"></selectSearch>
              </el-form-item>
              <el-form-item>
                <el-button
                  icon="el-icon-search"
                  type="primary"
                  class="marginLeft"
                  @click="getDataList(1)"
                >检索</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </el-row>
      <el-row>
        <div style="height: 50px; border-bottom: 1px solid #b0bde2">
          <div class="pull-right">
            <el-button plain type="primary" :disabled="isEnd" @click="endTarget">结束</el-button>
            <el-button plain type="primary" @click="templateManage">模板管理</el-button>
            <el-button plain type="primary" :disabled="isDisabledSum" @click="proposedSummary">拟定汇总</el-button>

            <el-button plain type="primary" @click="submit">提交</el-button>
          </div>
        </div>
      </el-row>
      <div style="margin:20px 0 0 0 ">
        <span>指标考核&nbsp;&nbsp;</span>
        <el-radio-group v-model="choosePerOrDept" @change="choosePerOrDeptChange">
          <el-radio :label="0">人员</el-radio>
          <el-radio :label="1">机构</el-radio>
        </el-radio-group>
      </div>
      <el-row>
        <el-table
          style="width: 100%;margin-top:20px"
          :data="dataList"
          border
          stripe
          ref="tableRef"
          header-align="center"
          v-loading="dataListLoading"
          @cell-click="unfoldBtn"
          @selection-change="handleSelectionChange"
        >
          <el-table-column :selectable="checkboxSelect" type="selection" width="55"></el-table-column>
          <el-table-column type="index" header-align="center" align="center" label="序号" width="55"></el-table-column>
          <el-table-column
            prop="assessUserName"
            align="center"
            label="员工编号"
            show-overflow-tooltip
            v-if="!choosePerOrDept"
          ></el-table-column>
          <el-table-column
            prop="assessRealName"
            align="center"
            label="考核对象"
            v-if="!choosePerOrDept"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span
                @click="showFillIn(scope.row)"
                style="color:rgb(14, 37, 140);cursor:pointer"
              >{{scope.row.assessRealName}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="assessRealName"
            align="center"
            label="考核对象"
            v-if="choosePerOrDept"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span
                @click="showFillIn(scope.row)"
                style="color:rgb(14, 37, 140);cursor:pointer"
              >{{scope.row.assessDeptName}}</span>
            </template>
          </el-table-column>

          <el-table-column
            prop="assessDeptName"
            align="center"
            label="部门"
            show-overflow-tooltip
            v-if="!choosePerOrDept"
          ></el-table-column>
          <el-table-column prop="itemNum" align="center" label="指标数量" show-overflow-tooltip></el-table-column>
          <el-table-column prop="name" align="center" label="考核名称" show-overflow-tooltip></el-table-column>
          <el-table-column prop="period" align="center" label="考核周期" show-overflow-tooltip></el-table-column>
          <!--<el-table-column prop="type" align="center" label="性质" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.type==0?'个人':'机构'}}</span>
            </template>
          </el-table-column>-->
          <el-table-column prop="state" align="center" label="状态" show-overflow-tooltip>
            <template slot-scope="scope">
              <div>
                <el-popover placement="top" width="660" trigger="hover">
                  <el-table :data="scope.row.gridData" max-height="400">
                    <el-table-column
                      width="260"
                      property="NAME_"
                      label="任务名称"
                      show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column
                      width="120"
                      property="ry_name"
                      label="处理人"
                      show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column
                      width="120"
                      property="START_TIME_"
                      label="开始时间"
                      show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column
                      width="120"
                      property="END_TIME_"
                      label="结束时间"
                      show-overflow-tooltip
                    ></el-table-column>
                  </el-table>
                  <span slot="reference">{{scope.row.state | transTargetState}}</span>
                  <el-button
                    type="primary"
                    icon="el-icon-search"
                    @click="showMessage(scope.row.processId)"
                  >流程跟踪</el-button>
                </el-popover>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <div class="avue-crud__pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageData.pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageData.pageSize"
            :total="pageData.totalPage"
            background
            layout="total, sizes, prev, pager, next, jumper"
          ></el-pagination>
        </div>
      </el-row>
    </el-row>

    <processTracking ref="processTracking" @processTrackingChildren="getDataList"></processTracking>
    <!--
    <UserCommonSelectTask v-if="usercommentTask" ref="userTask" @submitfy="submitfyTask"></UserCommonSelectTask>
    -->

    <!-- 拟定汇总 -->
    <el-row v-if="isPlanSummary">
      <TianBaoTable
        v-if="isPlanSummary == true"
        ref="tianbaotable"
        :tianBaoTable="tianBaoTable"
        :ids="ids"
        :radio="radio"
        @fnBackOldPage="fnBackOldPage"
        :ndids="ndids"
      ></TianBaoTable>
    </el-row>
    <targetFillin v-if="deptVisible" ref="deptNode" @closeDialog="closeDialog1" />
    <temanagementDialog :temanagementDialogData="temanagementDialogData"></temanagementDialog>
    <el-dialog title="请选择：" :visible.sync="chooseVisible" width="30%">
      <div style="display: flex; justify-content: space-around">
        <el-button type="warning" @click="customBtn">自定义流程</el-button>
        <el-dropdown>
          <el-button type="primary" size="small" v-if="temList.length > 0">
            模板
            <i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-button type="primary" size="small" v-else>暂无模板</el-button>
          <el-dropdown-menu v-if="temList.length > 0">
            <el-dropdown-item v-for="(item, index) in temList" :key="index">
              <span
                @click="temBtn(item)"
                v-loading.fullscreen.lock="fullscreenLoading"
              >{{ item.templateName }}</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-dialog>
    <customDialog :customDialogData="customDialogData" @customDialogChildren="customDialogChildren"></customDialog>
  </basic-container>
</template>

<script>
import targetExaminHead from "@/views/dailyExamine/components/targetExaminHead"; //状态头
import processTracking from "@/views/dailyExamine/components/zbProcessTracking"; //流程组件
//import UserCommonSelectTask from "@/views/hetongrenwu/components/usercomment-select";
import targetFillin from "./targetFillin.vue";
import temanagementDialog from "./temanagementDialog";
import TianBaoTable from "./htndhuizong-table.vue";
import customDialog from "./customDialog";
import selectSearch from "@/views/dailyExamine/components/select-search.vue"; //流程组件

import { examineList } from "@/api/dailyExamine/targetExamine/createAssessment"; //接口api
import {
  palnSubmit,
  indexEnd,
  getListMb,
  submitStatusAllfn,
  isflagzhibiao
} from "@/api/dailyExamine/targetExamine/indicatorFormulation";
import { mapGetters, mapState } from "vuex";
import { showMessage } from "@/api/activiti/task";
import store from "@/store";
export default {
  data() {
    return {
      isDisabledSum: true,
      headers: {
        Authorization: "Bearer " + store.getters.access_token
      },
      choosePerOrDept: 0,
      customDialogData: {},
      temList: [],
      tianBaoTable: {},
      ids: [],
      radio: "1",
      ndids: [],
      isPlanSummary: false,
      temanagementDialogData: {},
      deptVisible: false, //机构考核组件显隐
      searchheight: "0", //搜索组件的高度
      searchadvanced: true, //搜索的显隐
      searchForm: {}, //搜索对象
      dataListLoading: false, //表格是否加载
      dataList: [], //表格数组
      multipleSelection: [], //勾选数据
      pageData: {
        //分页对象
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0
      },
      isEnd: true,
      chooseVisible: false,
      //new
      hisTableData: [],
      actPicUrlList: [],
      actPicUrl: "",
      showPicDialog: false,
      usercommentTask: false,
      goSpVisible: false,
      iszbsh: Number,
      fullscreenLoading: false
    };
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  watch: {},
  components: {
    customDialog,
    TianBaoTable,
    temanagementDialog,
    targetExaminHead, //状态头.
    targetFillin, //填报组件
   // UserCommonSelectTask,
    processTracking,
    selectSearch
  },
  filters: {},
  created() {
    this.getDataList();
  },
  methods: {
    selectRow(selection, row) {
      let temp = [];
      if (selection.length > 1) {
        for (let i = 0; i < selection.length - 1; i++) {
          // debugger;
          if (selection[i].name == selection[i + 1].name) {
            temp.push(selection[i], selection[i + 1]);
          }
        }
        console.log(temp);
        temp.forEach(it => {
          this.$refs.tableRef.toggleRowSelection(it);
        });
      }
    },
    //  人员or机构选择改变
    choosePerOrDeptChange(val) {
      this.searchForm = {};
      this.dataList = [];
      this.getDataList();
    },
    checkboxSelect(row, rowIndex) {
      return true;
      // if (row.name == row.name) {
      //   return true;
      // } else {
      //   return false;
      // }
    },
    customDialogChildren(data) {
      this.getDataList();
    },
    temBtn(e) {
      this.fullscreenLoading = true;
      this.type = 1;
      var params = {
        type: this.type,
        iszbsh: this.iszbsh,
        rows: this.$refs.tableRef.selection,
        templateId: e.id
      };
      submitStatusAllfn(params).then(res => {
        this.fullscreenLoading = false;
        this.$message.closeAll();
        this.$message.success("操作成功");
        this.chooseVisible = false;
        this.getDataList();
      });
    },
    // 自定义流程
    customBtn() {
      this.chooseVisible = false;
      this.type = 2;
      this.customDialogData = {
        dialogVisible: true,
        type: this.type,
        iszbsh: this.iszbsh,
        rows: this.$refs.tableRef.selection,
        menuType: "zbnd"
      };
    },
    fnBackOldPage(data) {
      this.isPlanSummary = false;
      this.ids = [];
      this.multipleSelection = [];
      this.getDataList();
    },
    // 拟定汇总
    proposedSummary() {
      if (this.multipleSelection.length == 0) {
        this.$message.warning("请先选择数据");
      } else {
        this.isPlanSummary = true;
        this.tianBaoTable = {
          lastPage: "zbnd"
        };
      }
    },
    // 模板管理
    templateManage() {
      this.temanagementDialogData = {
        dialogVisible: true,
        type: "指标拟定"
      };
    },
    // 结束流程
    endTarget() {
      this.$confirm("是否结束当前指标?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // 确定
          let ids = this.multipleSelection.map(it => {
            return it.id;
          });
          indexEnd(ids.toString())
            .then(r => {
              if (r.data.code == 0) {
                this.$message.success("操作成功");
                this.getDataList();
              } else {
                this.$message.error("操作成功");
                this.getDataList();
              }
            })
            .catch(err => {
              this.getDataList();
            });
        })
        .catch(() => {
          // 取消
        });
    },
    //考核对象展示
    unfoldBtn(row) {
      row.rowshows = !row.rowshows;
      this.rednermsg = false;
      this.$nextTick(() => {
        this.rednermsg = true;
      });
    },
    //数组
    cutout(cellValue) {
      if (cellValue) {
        var c = cellValue.split(",");
        if (c.size < 1) {
          c.push("-");
          c.push("-");
        } else if (c.size < 2) {
          c.push("-");
        }
        return c;
      }
    },
    // 关闭机构考核新增弹窗
    closeDialog1() {
      this.deptVisible = false;
      this.getDataList();
    },
    // 显示填报
    showFillIn(data) {
      this.deptVisible = true;
      this.$nextTick(() => {
        this.$refs.deptNode.init(data);
      });
    },
    fngetListMb() {
      var params = {
        menuType: "指标拟定",
        tagName: "指标考核"
      };
      getListMb(params).then(res => {
        this.temList = res.data.data;
      });
    },
    // 提交
    submit(obj) {
      var rowsarr = [];
      var ids;
      if (obj == 1) {
        rowsarr = this.dataList;
      } else {
        const rows = this.$refs.tableRef.selection;
        if (rows.length == 0) {
          this.$message.closeAll();
          this.$message({
            type: "info",
            message: "请至少选择一条数据提交!"
          });
          return false;
        }
        rowsarr = this.$refs.tableRef.selection;
        ids = this.multipleSelection.map(it => {
          return it.id;
        });
      }
      /*if (this.multipleSelection.length == 0) {
        this.$message.warning("请先选择要提交的数据");
      } else {
        let ids = this.multipleSelection.map(it => {
          return it.id;
        });
          rowsarr = this.$refs.tableRef.selection;*/
      var params = {
        rows: rowsarr
      };
      isflagzhibiao(params).then(response => {
        if (!response.data.data) {
          this.$confirm("是否跳过指标审核", "提示", {
            confirmButtonText: "是",
            cancelButtonText: "否",
            type: "warning"
          })
            .then(res => {
              // console.log("跳过指标审核");
              this.iszbsh = 0;
              palnSubmit(ids.toString())
                .then(r => {
                  if (r.data.code == 0) {
                    this.$message.success("提交成功");
                    this.getDataList();
                  }
                })
                .catch(err => {});
            })
            .catch(() => {
              // 不跳过指标审核
              // console.log("不跳过指标审核");
              this.iszbsh = 1;
              this.chooseVisible = true;
              this.fngetListMb();
            });
        } else {
          this.$message.closeAll();
          this.$message({
            type: "error",
            message: "当前考核没有指标或者目标值为空,不能提交！"
          });
        }
      });
    },
    // 查询条件的展开关闭
    searchchangeadvanced() {
      this.searchadvanced = !this.searchadvanced;
      if (this.searchadvanced) {
        this.searchheight = "auto";
      }
    },
    // 查询列表
    getDataList() {
      ////
      examineList(
        Object.assign(
          {
            current: this.pageData.pageIndex,
            size: this.pageData.pageSize,
            type: this.choosePerOrDept
          },
          this.searchForm
        ),
        1
      )
        .then(r => {
            console.log(r.data.data.records)
          if (r.data.code == 0) {
            this.dataList = r.data.data.records.map(it => {
              it.rowshows = true;
              return it;
            });
            this.pageData.totalPage = r.data.data.total;
          }
        })
        .catch(err => {});
    },

    // 勾选数据
    handleSelectionChange(val) {
       console.log(val);
      this.multipleSelection = val;
      this.ids = val.map(it => {
        return it.id;
      });

      let flagNum = val.every(it => {
          return it.itemNum == 0;
      });

      let flag = val.some(it => {
      return it.isbr == 1;
      });

      if (val.length == 0) {
        this.isEnd = true;
      } else {
        if (!flag) {
            if(flagNum){
                this.isEnd = false;
            } else {
                this.isEnd = true;
            }

        } else {
          this.isEnd = true;
        }
      }
      //不是本人
      let isbrFlag = val.every(it => {
          return it.isbr == 0;
      });

      let temp = Object.assign(val);
      let obj = {};
      if (val.length !== 0) {
        let a = val.map(o => {
          return o.assessId + o.name;
        });

          //console.log(new Set(a));

          if (new Set(a).size > 1) {
              this.isDisabledSum = true;

          } else {

              if(!isbrFlag){

                  this.isDisabledSum = true;
              }else{
                  this.isDisabledSum = false;

              }

          }

      }
      if (val[0].type == 0) {
        this.radio = 1;
      } else {
        this.radio = 2;
      }
    },

    // 条数变化
    sizeChangeHandle(val) {
      this.pageData.pageSize = val;
      this.getDataList();
    },
    // 页码分页变化
    currentChangeHandle(val) {
      this.pageData.pageIndex = val;
      this.getDataList();
    },
    // 显示用户
    showUser(taskId) {
      this.taskId = taskId;
      this.usercommentTask = true;
      this.$nextTick(() => {
        this.$refs.userTask.init("", "zxshr", JSON.stringify([]), "htcj");
      });
    },
    showMessage(proId, row) {
      this.$refs.processTracking.init(proId);
    },
      //选择周期执行搜索功能
      getDataByPerid(period){
          this.searchForm.period = period ;
          this.getDataList() ;
      }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-select .el-select__tags {
  overflow: auto !important;
  //   min-height: 200px !important;
}
::v-deep.el-tag.el-tag--info {
  margin: 5px;
  border: 1px solid green;
}
</style>
